package com.retail.booking.client.impl;

import cn.hutool.core.util.ObjectUtil;
import com.retail.booking.client.BookingXiaohesuanClient;
import com.retail.booking.entity.BookingXiaohesuanSaveRequest;
import com.retail.booking.entity.request.BookingXiaohesuanRequest;
import com.retail.booking.entity.vo.BookingShareRecordVo;
import com.retail.booking.entity.vo.BookingXiaohesuanVo;
import com.retail.booking.service.BookingShareRecordService;
import com.retail.booking.service.BookingXiaohesuanService;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.service.AdminService;
import com.retail.system.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */
@Service("bookingXiaohesuanClient")
public class BookingXiaohesuanDefaultClientImpl implements BookingXiaohesuanClient {

    private final BookingXiaohesuanService bookingXiaohesuanService;
    private final BookingShareRecordService bookingShareRecordService;

    @Autowired
    private AdminService userService;

    private final DeptService deptService;

    public BookingXiaohesuanDefaultClientImpl(BookingXiaohesuanService bookingXiaohesuanService, BookingShareRecordService bookingShareRecordService, DeptService deptService) {
        this.bookingXiaohesuanService = bookingXiaohesuanService;
        this.bookingShareRecordService = bookingShareRecordService;

        this.deptService = deptService;
    }

    @Override
    public Result saveXiaohesuan(BookingXiaohesuanSaveRequest request) {
        try {
            BookingXiaohesuanRequest bookingXiaohesuanRequest = xhsSaveConvertXhsRequest(request);
            bookingXiaohesuanService.save(bookingXiaohesuanRequest);
            return Result.success("预约小核算成功！");
        } catch (Exception e) {
            return Result.fail("预约小核算失败！");
        }
    }

    @Override
    public Result findXiaohesuan(BookingXiaohesuanSaveRequest request) {
        BookingXiaohesuanVo bookingXiaohesuanVo = bookingXiaohesuanService.findXiaohesuan(request.getPhone(), request.getYuyueTime(), request.getDeptId());
        if (ObjectUtil.isNotEmpty(bookingXiaohesuanVo)) {
            return Result.success(bookingXiaohesuanVo);
        }
        return Result.success(null);
    }

    private BookingXiaohesuanRequest xhsSaveConvertXhsRequest(BookingXiaohesuanSaveRequest request) {
        BookingXiaohesuanRequest xhsRequest = new BookingXiaohesuanRequest();

        xhsRequest.setBookingTime(request.getYuyueTime());
        xhsRequest.setDelFlag(DelFlagEnum.NO.getValue());
        xhsRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        // 分享的用户编号
        Long shareUserId = request.getXhsShareUserId();
        Long shareConfigId = request.getXhsSharePosterId();
        xhsRequest.setShareUserId(shareUserId);
        xhsRequest.setUserId(request.getUserId());
        xhsRequest.setUserName(request.getUsername());
        xhsRequest.setUserPhone(request.getPhone());
        xhsRequest.setPosterId(shareConfigId);
        if (ObjectUtil.isNotEmpty(shareConfigId) && ObjectUtil.isNotEmpty(shareUserId)) {
            BookingShareRecordVo shareRecordVo = getLast1ShareRecord(shareUserId, shareConfigId);
            if (ObjectUtil.isNotEmpty(shareRecordVo)) {
                xhsRequest.setBookShareId(shareRecordVo.getId());
                xhsRequest.setUserCreatePosterId(shareRecordVo.getUserCreateShareId());
            }
        }

//        if (ObjectUtil.isNotEmpty(request.getUserId())) {
//            UserVo userVo = getUser(request.getUserId());
//            if (ObjectUtil.isNotEmpty(userVo)) {
//                xhsRequest.setDeptId(userVo.getDeptId());
//                DeptVo deptVo = deptService.getModel(userVo.getDeptId());
//                xhsRequest.setCrmDeptId(deptVo.getCrmDeptNo());
//                xhsRequest.setCrmStoreId(userVo.getCrmStoreNo());
//            }
//        }

        if (ObjectUtil.isNotEmpty(request.getDeptId())) {
            xhsRequest.setDeptId(request.getDeptId());
        }

        return xhsRequest;
    }

    /**
     * 得到用户分享的记录
     *
     * @param userId
     * @param posterId
     * @return
     */
    private BookingShareRecordVo getLast1ShareRecord(Long userId, Long posterId) {
        return bookingShareRecordService.getLast1ShareRecord(userId, posterId);
    }

    /**
     * 根据用户编号得到用户信息
     *
     * @param userId
     * @return
     */
    private AdminVo getUser(Long userId) {
        return userService.getModel(userId);
    }

    private AdminVo getUserByOpenid(String openid) {
        return userService.getUseByOpenId(openid);
    }
}
